package com.nave.princesslife.jogogenius;

import java.util.Random;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.nave.princesslife.R;

public class Game extends View implements Runnable {

	public Drawable coroa;

	Context context;
	private static final int INTERVAL = 700;
	public boolean running = true;
	public int pontos = 0;

	public int random;

	public Random rnd;

	private float cx;
	private float cy;
	private int iy2;
	private int ix2;
	private int dx;
	private int dy;

	Context c;

	// private Mover coroa;
	public boolean start = false;

	public Paint paint;
	public Paint paint2;

	int tempo = 5;

	public Thread minhaThread;

	com.nave.princesslife.jogogenius.GeniusActivity activity;

	int x1;
	int y1;
	int largura1;
	int altura1;

	private int w;
	private int h;

	public Game(Context context) {
		super(context);

		coroa = context.getResources().getDrawable(R.drawable.ic_launcher);

		this.context = context;

		rnd = new Random();

		paint = new Paint();
		paint.setColor(Color.BLACK);

		paint2 = new Paint();
		paint2.setColor(Color.WHITE);

		x1 = 50; // vermelho Escuro
		y1 = 20;

		altura1 = coroa.getIntrinsicHeight();
		largura1 = coroa.getIntrinsicWidth();

		random = rnd.nextInt(4 - 0) + 0;

		Log.i("random: ", random + "");

		minhaThread = new Thread(this);
		minhaThread.setPriority(Thread.MIN_PRIORITY);
		minhaThread.start();

	}

	@Override
	protected void onSizeChanged(int width, int height, int oldw, int oldh) {
		super.onSizeChanged(width, height, oldw, oldh);

		this.w = getWidth();
		this.h = getHeight();
		cx = w / 2;
		cy = h / 2;

	}

	public void run() {
		while (running) {
			try {
				Thread.sleep(INTERVAL);

				tempo--;

				if (tempo < 0)
					tempo = 0;

				// pontos++; //faz um tempo x)
			} catch (Exception e) {
				Log.e("Jogo", "Sleep da Thread");
			}
			update();

		}
	}

	public void update() {

		cx += dx;
		cy += dy;

		if (cx + ix2 > w || cx - ix2 < 0) {
			dx *= -1;
			cx += dx;
		}

		if (cy + iy2 > h || cy - iy2 < 0) {
			dy *= -1;
			cy += dy;
		}
		postInvalidate();
	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		canvas.drawPaint(paint);

		coroa.setBounds(x1, y1, x1 + largura1, y1 + altura1);
		coroa.draw(canvas);

		int x = (int) (Math.random() * (largura1 * 2 - 25));
		int y = (int) (Math.random() * (altura1 * 2 - 25));

		x1 = x + 5;
		y1 = y + 10;

		canvas.drawText("Pontos: " + pontos, 100, 10, paint2);
		canvas.drawText("Tempo: " + tempo, 0, 10, paint2);

	}

	public boolean onTouchEvent(MotionEvent motionEvent) {
		int x = (int) motionEvent.getX();
		int y = (int) motionEvent.getY();

		if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
			if (coroa.copyBounds().contains(x, y)) {

				int r = (int) (Math.random() * (largura1 * 2 - 25));
				int g = (int) (Math.random() * (altura1 * 2 - 25));

				x1 = r + random;
				y1 = g + random;

				pontos++;

				invalidate(); // atualiza o canvas
			}
		}
		return super.onTouchEvent(motionEvent);

	}
}